var ArmatureController = require('geoArmatureController');
var TweenController = require('geoTweenMoveTo');
var SequenceRunner = require('geoSequenceRunner');
var SequenceData = require('geoArmatureSequenceData');
var CompleteType = SequenceData.CompleteType;

var ArmatureSequencer = cc.Class({
    extends: cc.Component,
    properties: {
        defaultArmatureController: {
            default: null,
            type: ArmatureController,
            tooltip: '默认龙骨控制器'
        },
        defaultTweenController: {
            default: null,
            type: TweenController,
            tooltip: '默认变换控制器'
        },
        sequenceDatas: {
            default: [],
            type: [SequenceData],
            tooltip: '动画序列数据'
        },
        playOnLoad: {
            default: false,
            tooltip: '是否加载及播放运行'
        }
    },
    //
    onLoad() {
        if (this.playOnLoad === true)
            this.play();
    },
    /**
     * 播放整个序列
     * @param {*} defaultArmatureCotnroller 
     * @param {*} defaultTweenController 
     * @param {*} sequenceDatas 
     */
    play(defaultArmatureCotnroller, defaultTweenController, sequenceDatas) {
        //
        defaultArmatureCotnroller = defaultArmatureCotnroller || this.defaultArmatureController;
        defaultTweenController = defaultTweenController || this.defaultTweenController;
        sequenceDatas = sequenceDatas || this.sequenceDatas;
        //
        var sq = new SequenceRunner();
        var funcs = [];
        //
        sequenceDatas.forEach(sequenceData => {
            funcs.push(
                this.playData.bind(this,
                    defaultArmatureCotnroller,
                    defaultTweenController,
                    sequenceData,
                    sq.callNext)
                    // sq.next.bind(sq))
            );
        }, this);
        //
        sq.run(funcs);
    },
    /**
     * 播放一个序列数据
     * @param {*} defaultArmatureCotnroller 
     * @param {*} defaultTweenController 
     * @param {*} sequenceData 
     */
    playData(defaultArmatureCotnroller, defaultTweenController, sequenceData, complete) {
        var ac = sequenceData.animController || defaultArmatureCotnroller;
        var tc = sequenceData.tweenController || defaultTweenController;
        var event = new cc.Event.EventCustom('geo-armature-sequencer-complete');
        event.target = this.node;
        event.com = this;
        // 预处理结束回调
        var animComplete = (() => {
            //
            sequenceData.sequenceCompleteType === CompleteType.ANIMATION
                && complete
                && complete();
        }).bind(this);

        var tweenComplete = (() => {
            //
            sequenceData.sequenceCompleteType === CompleteType.TWEEN
                && complete
                && complete();
        }).bind(this);
        //
        var animDuration = 0;
        // 处理动画
        if (sequenceData.animData != null) {
            var animState = sequenceData.animData.play(animComplete, ac, event);
            animState && (animDuration = animState.totalTime * animState.timeScale);
        }
        // 处理变换
        if (sequenceData.tweenData != null)
            sequenceData.tweenData.play(tweenComplete, tc, event, animDuration || sequenceData.tweenData.tweenDuration);
    },
    /**
     * 由事件触发时使用的play。
     * 常用于编辑器中绑定事件
     */
    playOnEvent(){
        this.play();
    }
});

module.exports = ArmatureSequencer;
